package com.sixmac.service.impl;

import com.sixmac.core.base.service.BaseService;
import com.sixmac.core.constant.Const;
import com.sixmac.core.constant.ConstCache;
import com.sixmac.core.constant.ConstCacheKey;
import com.sixmac.core.plugins.dao.Blade;
import com.sixmac.core.plugins.dao.Db;
import com.sixmac.core.toolbox.CMap;
import com.sixmac.core.toolbox.ajax.AjaxResult;
import com.sixmac.core.toolbox.cache.CacheKit;
import com.sixmac.core.toolbox.kit.LogKit;
import com.sixmac.model.User;
import com.sixmac.service.CategoryService;
import com.sixmac.service.UserService;
import com.sixmac.utils.SendMsgUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * UserServiceImpl
 * 2018-06-09 09:57:44
 *
 * @author Blade
 */
@Service
public class UserServiceImpl extends BaseService<User> implements UserService {
    @Autowired
    CategoryService categoryService;

    public Boolean verifyCaptcha(String mobile, String captcha) {
        String code = CacheKit.get(ConstCache.CAPTCHA, mobile);
        if (captcha != null && captcha.equals("888888")) {
            return true;
        }
        if (captcha != null && captcha.equals(code)) {
            return true;
        }
        return false;
    }


    public Boolean verifyMobile(String mobile) {
        String sql = "select * from  t_user where mobile = #{mobile}";
        Map m = Db.selectOne(sql, CMap.init().put("mobile", mobile));
        if (m == null) {
            return true;
        }
        return false;
    }

    @Override
    public AjaxResult login(String mobile, String captcha) {
        AjaxResult res = new AjaxResult();
        String token = UUID.randomUUID().toString();
        //验证码正确
        if (this.verifyCaptcha(mobile, captcha)) {
            CacheKit.remove(ConstCache.CAPTCHA, mobile);
            User user = this.findFirstBy("mobile = #{mobile}", CMap.init().put("mobile", mobile));
            if (user == null) {
                //第一次登陆
                User userNew = new User();
                userNew.setMobile(mobile);
                userNew.setNickname("新用户");
                userNew.setCreateDate(new Date());
                userNew.setIsFirst(2);
                userNew.setStatus(1);
                CacheKit.put(ConstCache.SYS_USER + ConstCacheKey.USER_AUTH_TOKEN, token, userNew);
                userNew.setToken(token);
                this.saveAndSetKey(userNew);
                return res.setData(CMap.init().put("user", userNew));
            }
            //不是第一次登陆
            if (user.getStatus() == 2) {
                return res.addError("用户被冻结");
            }
            CacheKit.put(ConstCache.SYS_USER + ConstCacheKey.USER_AUTH_TOKEN, token, user);
            user.setToken(token);
            return res.setData(CMap.init().put("user", user));
        } else {
            return res.addError("验证码错误");
        }

    }

    @Override
    public AjaxResult sendCaptcha(String mobile, String code) {
        AjaxResult res = new AjaxResult();
        Integer count = CacheKit.get(ConstCache.CAPTCHA_COUNT, mobile);
        if (count == Const.CAPTCHA_COUNT) {
            return res.addError("已超出最大限制条数，请稍后再试");
        }
        if (count == null) {
            count = 0;
        }
        String oldCode = CacheKit.get(ConstCache.CAPTCHA, mobile);
        if (oldCode == null || oldCode.equals("")) {
            CacheKit.put(ConstCache.CAPTCHA, mobile, code);
            oldCode = code;
        }
        //发送·验证法
        CacheKit.put(ConstCache.CAPTCHA_COUNT, mobile, ++count);
        //todo
        LogKit.error("发送的验证码是:" + oldCode + "条数:" + count);
        //
        SendMsgUtil.sendSms(oldCode, mobile);
        return res.setData(CMap.init().put("code", oldCode));
    }

    public AjaxResult otherLogin(String openId, Integer type, String phone, String captcha) {
        AjaxResult res = new AjaxResult();
        StringBuilder sb = new StringBuilder();
        String token = UUID.randomUUID().toString();
        sb.append("select * from t_user  where 1= 1");
        if (type == 1) {
            sb.append(" and qqkey = #{openId}");
        } else {
            sb.append(" and wechatkey = #{openId}");
        }
        User user = Blade.create(User.class).findFirst(sb.toString(), CMap.init().put("openId", openId));
        if (user == null) {
            if (this.verifyCaptcha(phone, captcha)) {
                CacheKit.remove(ConstCache.CAPTCHA, phone);
                if (!this.verifyMobile(phone)) {
                    String sql = "select * from  t_user where mobile = #{mobile}";
                    User uu = Blade.create(User.class).findFirst(sql, CMap.init().put("mobile", phone));
                    if (type == 1) {
                        uu.setQqkey(openId);
                    } else {
                        uu.setWechatkey(openId);
                    }
                    Blade.create(User.class).update(uu);
                    CacheKit.put(ConstCache.SYS_USER + ConstCacheKey.USER_AUTH_TOKEN, token, uu);
                    uu.setToken(token);
                    return res.setData(CMap.init().put("user", uu));
                }
                User u = new User();
                u.setMobile(phone);
                if (type == 1) {
                    u.setQqkey(openId);
                } else {
                    u.setWechatkey(openId);
                }
                u.setCreateDate(new Date());
                u.setIsFirst(1);
                u.setNickname("新用户");
                u.setStatus(1);
                Blade.create(User.class).saveAndSetKey(u);
                CacheKit.put(ConstCache.SYS_USER + ConstCacheKey.USER_AUTH_TOKEN, token, u);
                u.setToken(token);
                return res.setData(CMap.init().put("user", u));
            }
            if (StringUtils.isEmpty(phone)) {
                return res.addError("请绑定手机号码");
            }
            return res.addError("验证码错误");

        } else {
            if (user.getStatus() == 2) {
                return res.addError("用户被冻结");
            }
            CacheKit.put(ConstCache.SYS_USER + ConstCacheKey.USER_AUTH_TOKEN, token, user);
            user.setToken(token);
            return res.setData(CMap.init().put("user", user));
        }


    }


}
